//
//  SettingViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/29.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit

class SettingViewController: SettingItemViewController {
    
    let email = TableViewItem.init(title: "修改邮箱".localized)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "设置".localized
        tableView.backgroundColor = #colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1)
        
        email.cellSelect = { [unowned self] in
            self.changeEmail()
        }
        
        ApiManager.userInfo().loadCache()
            .dataModelMap(PersonalModel.self)
            .repeatWhen(NotificationCenter.default.rx.notification(.userInfoRefresh))
            .subscribe(onNext: { [weak self] (model) in
                self?.email.detailTitle = model.email
                self?.tableView.reloadData()
            }).disposed(by: rx.disposeBag)
    }
    
    override func sectionArray() -> [TableViewSection] {
        let info = TableViewItem.init(title: "修改个人信息".localized)
            .cellSelect { [unowned self] in
                self.navigationController?.pushViewController(ChangeUserInfoViewController(), animated: true)
        }
        
        let url = TableViewItem.init(title: "用户协议和隐私政策".localized)
            .cellSelect { [unowned self] in
                let vc = BaseWebViewController.init(url: APPConfig.protocolUrl)
                self.navigationController?.pushViewController(vc, animated: true)
        }
        
        let changeLanguage = TableViewItem.init(title: "语言".localized)
            .itemSetUp {
                $0.detailTitle = AppCache.shared.object(for: APPUserDefault.self).userSelectLanguage?.text ?? "English"
            }.cellSelect { [unowned self] in
                let alert = UIAlertController.init(title: "切换语言".localized, message: nil, preferredStyle: .actionSheet)
                let array: [UserSelectLanguage] = [.english, .chinese, .de, .vi]
                array.forEach { (type) in
                    alert.addAction(UIAlertAction.init(title: type.text, style: .default, handler: { (_) in
                        APPHud.shared.showLoading(text: "请稍后".localized, delay: 0.8) {
                            AppCache.shared.updateObject(for: APPUserDefault.self) {
                                $0.userSelectLanguage = type
                            }
                            let rootVC = AEAppDelegateTool.sharedInstance().getMainTabBarVC();
                            // MainTabBarViewController
                            UIApplication.shared.keyWindow?
                                .setRootViewController(rootVC)
                        }
                    }))
                }
                alert.addAction(UIAlertAction.init(title: "取消".localized, style: .cancel, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
        return [TableViewSection.section(items: [changeLanguage]),
                TableViewSection.section(items: [info, email]),
                TableViewSection.section(items: [url])]
    }
    
    @objc func changeEmail() {
        let vc = ChangeEmailViewController.init(email: email.detailTitle)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .baseColor
    }
}
